{% extends 'base.html' %}

{% block content %}
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend id="titleLeg"> 地址添加</legend>
    </fieldset>

    <div class="col-md-8">

        <form method="post" id="mapforms">
            <div class="form-group">
                <label for="serialnumber" class="col-sm-2 control-label">IP地址</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="address" placeholder="address"
                           name="address" maxlength="15">
                </div>
                <div class="col-md-3"><p class="error-message"></p></div>
            </div>

            <div class="col-md-offset-2" style="padding-top: 30px">
                <button type="button" class="btn btn-primary btn-block" id="butn">提交</button>
            </div>
        </form>
    </div>

    <div class="col-md-4"></div>

{% endblock %}

{% block script %}
    {{ super() }}

    <script>
        $(function () {
            // 渲染抬头名称
            addName(3);
            $("#butn").on('click', function () {
                var address = $("#address").val();
                if (!checkip(address)) {
                    $(".error-message").html('不是正确的IPV4地址');
                    return false
                } else {
                    var tablename = document.location.pathname.split('/')[3];
                    var position = document.location.pathname.split('/')[4];
                    $.ajax({
                        url: '/fwmap/fwIndexApi/' + tablename + '/' + position,
                        type: "post",
                        async: false,
                        data: {"address": address},
                        success: function (data) {
                            if (data.code == 10000) {
                                swal({
                                    title: "创建成功！",
                                    text: "2秒后自动返回地址首页。",
                                    timer: 1500,
                                    showConfirmButton: false
                                }, function () {
                                    setInterval(function () {
                                        window.history.back(-1);
                                        {#window.open("/fwmap/fwaddindex/" + tablename + '/' + position, "_self")#}
                                    }, 1000)
                                });
                            } else if (data.code == 10001 ){
                                sweetAlert("哎呦……", "地址已存在, 添加失败！","error");
                            }
                        }
                    })
                }
            })
        })
    </script>
{% endblock %}